//
//  JsonParser.m
//  Training
//
//  Created by michael shen on 15/6/17.
//  Copyright (c) 2015年 coolem. All rights reserved.
//

#import "JsonParser.h"

@implementation JsonParser
+ (JsonParser *)defaultParser{
    static JsonParser *parser = nil;
    if (parser == nil) {
        parser = [[JsonParser alloc] init];
    }
    return parser;
}

- (NSError *)getResultWithResponseData:(NSData *)responseData{
    if (responseData == nil) {
        return [NSError errorWithDomain:@"" code:0 userInfo:nil];
    }
    NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:nil];
    NSString *status = [data objectForKey:@"status"];
    if ([status intValue] == 1) {
        return nil;
    }
    if ([status isMemberOfClass:[NSString class]]) {
        if ([status isEqualToString:@"OK"]) {
            return nil;
        }
    }
    NSString *code = [data objectForKey:@"code"];
    if ([code intValue] == 0 && code != nil) {
        return nil;
    }
    
    NSString *domain = [data objectForKey:@"message"];
    if (domain == nil) {
//        domain = UnNullString([[data objectForKey:@"results"] objectForKey:@"message"]);
    }
    return [NSError errorWithDomain:domain code:[[data objectForKey:@"status"] intValue] userInfo:nil];
}
@end
